package com.umeox.watch.moto.sos;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import com.tencent.mmkv.MMKV;
import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.sos.utils.MicrophoneUtils;

import java.util.List;

/**
 * 按键处理方法
 */
public class SosReceiver extends BroadcastReceiver {
    public static final String ACTION_KEY_SOS = "com.wherecom.intent.action.ACTION_SOS";
    public static final String ACTION_FACTORY_MODE = Constants.APPLICATION_ACTION + ".Factorymode";
    private static final String TAG = "SosReceiver";

    public static void startActivityFromNoAct(Context ctx, Intent intent) {
        PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        try {
            pi.send();
        } catch (CanceledException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onReceive(final Context context, Intent intent) {
        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            return;
        }

        if (ACTION_FACTORY_MODE.equalsIgnoreCase(action)
                || (!DataProvider.isActivated() && ACTION_KEY_SOS.equalsIgnoreCase(action))) {
            //打开工程模式
            intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if(BuildConfig.FLAVOR.equalsIgnoreCase("Kid_Phone")){
                //org.chivin.factorymode/.FactoryMode
                intent.setComponent(new ComponentName("org.chivin.factorymode", "org.chivin.factorymode.FactoryMode"));
            }else{
                intent.setComponent(new ComponentName("com.sprd.validationtools", "com.sprd.validationtools.ValidationToolsMainActivity"));
            }
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(intent);
            } else {
                Logger.e(TAG, "进入工程模式失败，未找到组件");
            }
            return;
        }

        boolean isInCall = Settings.Global.getInt(context.getContentResolver(),"isInVideoCall",0) == 1;
        Logger.e(TAG,"isInCall=" + isInCall);
        if(isInCall){
            Logger.e(TAG, "the device is in video call");
            return;
        }


        List<ContentValues> contacts = DataProvider.getContacts();

        if (contacts == null || contacts.isEmpty()) {
            Log.d(TAG, "SosReceiver&onReceive no contacts");
            return;
        }

        boolean hasSos = MMKV.defaultMMKV().decodeBool(BaseApp.SOS_STATUS, false);
        Log.d(TAG, "SosReceiver&onReceive hasSoS:" + hasSos);
        if (hasSos) {
            return;
        }

        boolean isMicroPhoneInUse = MicrophoneUtils.isMicrophoneInUse();
        Log.d(TAG, "SosReceiver&onReceive isMicroPhoneInUse:" + isMicroPhoneInUse);
        if (isMicroPhoneInUse) {
            return;
        }

        if (ACTION_KEY_SOS.equals(intent.getAction())) {
            // SOS键按下
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(200);
            intent.setClass(context, SOSActivity.class);
            startActivityFromNoAct(context, intent);
        }
    }
}
